package org.test.token;

import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.server.authorization.token.JwtEncodingContext;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer;
import org.springframework.stereotype.Service;
import org.test.granter.CustomAuthorizationGrantType;

/**
 * @Author xkx
 * @Description 透明token扩展
 * @Date 2024/11/28 21:33
 * @Param
 * @return
 **/
@Service
public class ChickSelfContainedTokenEnhancer implements OAuth2TokenCustomizer<JwtEncodingContext> {
    @Override
    public void customize(JwtEncodingContext context) {
        context.getClaims().claims(claims -> {
            if (AuthorizationGrantType.CLIENT_CREDENTIALS.equals(context.getAuthorizationGrantType())) {
                claims.put("clientId", "1");
            } else if (CustomAuthorizationGrantType.PASSWORD.equals(context.getAuthorizationGrantType())) {
                claims.put("userId", "2");
            } else if (AuthorizationGrantType.REFRESH_TOKEN.equals(context.getAuthorizationGrantType())) {
                claims.put("clientId", "3");
            } else if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(context.getAuthorizationGrantType())) {
                claims.put("clientId", "4");
            }
        });
    }
}
